/*
 * Peripheral Simulator (PSim)
 *
 * Copyright (c) 2008 Avadh Patel
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

//PRAM module

#ifndef PRAM_H
#define PRAM_H

#include <psim.h>
#include <psim_lib.h>

typedef struct pram_cache_line_t pram_cache_line_t;

struct pram_cache_line_t
{
    uint64_t tag_addr;
    int modified_flag;
    int lru_count;
} ;

void init_pram();

int access_pram(uint64_t addr, uint8_t type);

void access_pram_sector(int64_t sector_num, uint8_t type);

void set_new_pram_entry(uint64_t addr, uint8_t type);

void copy_pram_entry(pram_cache_line_t* source, pram_cache_line_t* dest);

//Victim Buffer
typedef struct victim_buf_entry_t victim_buf_entry_t;

struct victim_buf_entry_t
{
    pram_cache_line_t* cache_line;
    victim_buf_entry_t* lchild;
    victim_buf_entry_t* rchild;
};

void init_victim_buffer();

void add_victim_entry(pram_cache_line_t* pram_entry);

victim_buf_entry_t* search_victim_entry(uint64_t addr);

void delete_victim_entry(uint64_t addr);

void sort_victim_buffer();

victim_buf_entry_t* create_new_vb_entry(pram_cache_line_t* pram_entry);

void flush_victim_buffer();

#endif //PRAM_H
